#Make sure we can find buildspace generated messages
include_directories(${CMAKE_BINARY_DIR}/gen/cpp)

if(@PACKAGE_NAME@_SOURCE_DIR)
  find_program_required(GENCPP_BIN gen_cpp.py PATHS ${@PACKAGE_NAME@_SOURCE_DIR}/scripts)
  set(GENCPP_TEMPLATE_DIR ${@PACKAGE_NAME@_SOURCE_DIR}/scripts)
else()
  find_program_required(GENCPP_BIN gen_cpp.py
    PATHS @CMAKE_INSTALL_PREFIX@/share/gencpp/bin)
  set(GENCPP_TEMPLATE_DIR @CMAKE_INSTALL_PREFIX@/share/gencpp)
endif()

# Generate .msg->.h for cpp
# The generated .h files should be added ALL_GEN_OUTPUT_FILES_cpp
macro(_generate_msg_cpp ARG_PKG ARG_MSG ARG_IFLAGS ARG_MSG_DEPS ARG_GEN_OUTPUT_DIR)

  #Create input and output filenames
  get_filename_component(MSG_NAME ${ARG_MSG} NAME)
  get_filename_component(MSG_SHORT_NAME ${ARG_MSG} NAME_WE)

  set(MSG_GENERATED_NAME ${MSG_SHORT_NAME}.h)
  set(GEN_OUTPUT_FILE ${ARG_GEN_OUTPUT_DIR}/${MSG_GENERATED_NAME})

  assert(CATKIN_ENV)
  add_custom_command(OUTPUT ${GEN_OUTPUT_FILE}
    DEPENDS ${GENCPP_BIN} ${ARG_MSG} ${ARG_MSG_DEPS}
    COMMAND ${CATKIN_ENV} ${PYTHON_EXECUTABLE} ${GENCPP_BIN} ${ARG_MSG}
    ${ARG_IFLAGS}
    -p ${ARG_PKG}
    -o ${ARG_GEN_OUTPUT_DIR}
    -e ${GENCPP_TEMPLATE_DIR}
    COMMENT "Generating C++ code from ${ARG_PKG}/${MSG_NAME}"
    )

  list(APPEND ALL_GEN_OUTPUT_FILES_cpp ${GEN_OUTPUT_FILE})

endmacro()

#gencpp uses the same program to generate srv and msg files, so call the same macro
macro(_generate_srv_cpp ARG_PKG ARG_SRV ARG_IFLAGS ARG_MSG_DEPS ARG_GEN_OUTPUT_DIR)
  _generate_msg_cpp(${ARG_PKG} ${ARG_SRV} "${ARG_IFLAGS}" "${ARG_MSG_DEPS}" ${ARG_GEN_OUTPUT_DIR})
endmacro()

macro(_generate_module_cpp)
  # the macros, they do nothing
endmacro()

set(gencpp_INSTALL_DIR include)
